package com.ttxz.ttxztest

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException

class OracleTestActivity : AppCompatActivity() {
    private val dbConnectUrl = "jdbc:oracle:thin:@192.168.0.152:1521:test"
    private val user = "test"
    private val password = "123456"
    var con: Connection? = null

    lateinit var tvContent: TextView

    val sbContent = StringBuilder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sql_server_test)
        Class.forName ("oracle.jdbc.driver.OracleDriver");
        tvContent = findViewById<TextView>(R.id.tv_content)

    }

    /**
     * 连接数据库
     */
    fun click_connect_db(view: View) {
        runBlocking(Dispatchers.IO) {
             con = DriverManager.getConnection(dbConnectUrl, user, password)
        }
        Log.e("@@@", "连接是否成功===" + con)
        tvContent.text = "连接结果==${con != null}"
    }


    fun click_add(view: View) {
        sbContent.clear()
        runBlocking {
            val result = async(Dispatchers.IO) {
                val stmt = con?.createStatement()
                val sql = "INSERT INTO TEST.T_USER_TEMP VALUES(100,'旺旺',15)"
                try {
                    val result = stmt?.executeUpdate(sql) as Int
                    result > 0
                } catch (e: Exception) {
                    false
                }
            }
            sbContent.append("插入数据结果===").append(result.await())
            tvContent.text = sbContent.toString()
        }
    }

    fun click_remove(view: View) {
        sbContent.clear()
        runBlocking {
            val result = async(Dispatchers.IO) {
                val stmt = con?.createStatement()
                val sql = "delete from TEST.T_USER_TEMP where Name='旺旺'"
                try {
                    val result = stmt?.executeUpdate(sql) as Int
                    result > 0
                } catch (e: Exception) {
                    false
                }
            }
            sbContent.append("删除数据结果===").append(result.await())
            tvContent.text = sbContent.toString()
        }
    }

    fun click_update(view: View) {
        sbContent.clear()
        runBlocking {
            val result = async(Dispatchers.IO) {
                val stmt = con?.createStatement()
                val sql = "UPDATE TEST.T_USER_TEMP SET AGE=20 where Name='旺旺'"
                try {
                    val result = stmt?.executeUpdate(sql) as Int
                    result > 0
                } catch (e: SQLException) {
                    false
                }
            }
            sbContent.append("更新数据结果===").append(result.await())
            tvContent.text = sbContent.toString()
        }
    }

    /**
     * 查询数据库
     */
    fun click_query(view: View) {
        sbContent.clear()
        runBlocking {
            val result = async(Dispatchers.IO) {
                val stmt = con?.createStatement()
                val sql = "SELECT * FROM TEST.T_USER_TEMP"
                val result = stmt?.executeQuery(sql)
                while (result?.next() == true) {

                    sbContent.append(result.getString(1)).append(",").append(result.getString(2))
                        .append(",\n")
                }
                sbContent.toString()
            }
            result.await()
            tvContent.text = sbContent.toString()
        }
    }

}